﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MongoCamp;
using MongoDB.Driver.Builders;
using MongoDB.Bson;

//using MongoDB.Bson;

namespace SimpleDemo
{
    class Program
    {
        public static bool TryGetValue<T>(BsonDocument document, string name, out T value)
        {
            value = default(T);
            BsonValue rawValue;

            if (!document.TryGetValue(name, out rawValue)) return false;

            try
            {
                value = (T) To<T>(rawValue);
                return true;
            }
            // ReSharper disable once EmptyGeneralCatchClause
            catch { }
            return false;

        }

        internal static object To<T>(BsonValue bsonValue)
        {
            var conversionType = typeof(T);
            if (conversionType == typeof(string))
            {
                return bsonValue.ToString();
            }
            return Convert.ChangeType(bsonValue, conversionType);
        }

        public static T GetValue<T>(BsonDocument document, string name)
        {
            throw new NotImplementedException();
        }

        static void Main(string[] args)
        {



            var document = new BsonDocument()
            {
                {"Title","Title is test"},
                {"Content","Content is test"},
                {"CreatedTime",DateTime.Now}
            };

           

            Blog.Update(0, document);


            Blog.DorpCollection();
            BlogReview.DorpCollection();

            // save code
            var b1 = new Blog { BlogId = 1, Title = "I'm title", Content = "content" };
            b1.Save(); 
            // Blog.Save(b1);
            b1.Save(2); // new entity, BlogID = 2
            //Blog.Insert(b1);


            // find & save code 
            Blog entity1 = Blog.FindOne(1);
            entity1.Content = "test content 1";
            entity1.Save();

            BsonDocument bson1 = Blog.FindOneAsBson(2);
            bson1["Content"] = "test content 2";
            Blog.Save(bson1);


            // find list code
            IList<Blog> entityList = Blog.FindList(Query.EQ("Title", "I'm title"));

            foreach (var entity in entityList)
            {
                Console.WriteLine(entity.Content);
            }

            IList<BsonDocument> bsonList = Blog.FindListAsBson(
                Query.EQ("Title", "I'm title"),
                Fields.Include("BlogID", "Title", "Content"), 
                SortBy.Descending("CreatedTime"), 20);

            foreach (var bson in bsonList)
            {
                Console.WriteLine(bson["Content"].AsString);
            }

            // remove code
            Blog.Remove(1); // _id = 1 or BlogID = 1
            Blog.Remove(Query.GT(PrimaryKey.Field, 0)); // BlogID > 0

            #region Linq Code

            // Linq Code
            //var b2 = Blog.FindOne(a => a.BlogID == 1 && a.Title == "title").OrderBy(b => b.CreatedTime == -1);
            //var q = new MongoQuery(@"blog");
            //var q1 = from b1 in q
            //         where b1[PrimaryKey.Field] == 1
            //         select
            //             new
            //                 {
            //                     BlogID = b1[PrimaryKey.Field].AsInt32,
            //                     Title = b1["Title"].AsString,
            //                     Content = b1["Content"].AsString
            //                 };
            //foreach (var item1 in q1)
            //{
            //    Console.WriteLine(item1.Title);
            //}

            //var qt = new MongoQuery<Blog>();
            //var qt1 = from  b2 in qt where b2.PrimaryKeyID == 1 select b2;

            #endregion Linq Code

            // Insert Batch
            var review1 = new BlogReview { BlogID = 1, Nickname = "user1", ReviewContent = "1111" };
            var review2 = new BlogReview { BlogID = 2, Nickname = "user2", ReviewContent = "2222" };
            //BlogReview.Insert(review1);
            //BlogReview.Insert(review2);
            BlogReview.InsertBatch(new[] { review1, review2 });


            // Other: logDB, BsonDocument
            BlogVisit visit1 = new BlogVisit()
                {
                    PrimaryKeyID = PrimaryKey.GenerateNewGuid(),
                    UserIP = "192.168.1.1",
                    UserAgent = "Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; rv:11.0)"
                };
            BlogVisit.Save(visit1);

            var visit2 = new BsonDocument
                {
                    {"Uptime", DateTime.Now},
                    {"UserIP", "127.0.0.1"},
                    {"UserAgent", "Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; rv:11.0)"},
                };
   
            BlogVisit.Insert(visit2);
            //BlogVisit.Insert(visit2); //error

            Console.WriteLine("\tThe End.");
            Console.ReadKey();
        }

    }
}
